/**表单验证**/
$("#form").form({
    fields: {
        title: {
            identifier: 'title',
            rules: [{
                type: 'empty',
                prompt: '标题：请输入博客标题！'
            }]
        },
        content: {
            identifier: 'content',
            rules: [{
                type: 'empty',
                prompt: '内容：请输入博客内容！'
            }]
        },
        typeName: {
            identifier: 'typeName',
            rules: [{
                type: 'empty',
                prompt: '标签：请输入博客分类！'
            }]
        },
        firstPicture: {
            identifier: 'firstPicture',
            rules: [{
                type: 'empty',
                prompt: '标签：请输入博客首图！'
            }]
        }
    }
});
/**加载分类和tag*/
$(function() {
    //alert("1");
    selectTypes1();
    selectTags();
});
//获取所有type
function selectTypes1(){
    $.ajax({
        url:'/type/selectTypes',
        type: 'GET',
        success: function(data){
            $.each(data.data,function(i,j){
                $("#types1").append("<div class=\"item\" data-value=\""+j.code+"\">"+j.name+"</div>")
            })
        }
    })
}
//获取所有tag
function selectTags(){
    $.ajax({
        url:'/tag/selectTags',
        type: 'GET',
        success: function(data){
            $.each(data.data,function(i,j){
                $("#tags").append("<div class=\"item\" data-value=\""+j.code+"\">"+j.name+"</div>")
            })
        }
    })
};
//修改
//更改提交时的发布和保存
$("#save-bth").on("click",function() {
    $("input[name=published]").val(false);

    var published=$("input[name=published]").val();
    var flag=$("input[name=flag]").val();
    var title =$("input[name=title1]").val();
    var content =$("textarea[name=content]").val();
    var typeCode =$("input[name=tname]").val();
    var tagCode =$("input[name=tag]").val();
    var firstPicture =$("input[name=firstPicture1]").val();
    var id =$("input[name=uid]").val();
    var blogCode =$("input[name=blogCode]").val();
    var recommend =$("input[name=recommend]").is(":checked");
    var shareStatement =$("input[name=shareStatement]").is(":checked");
    var appreciation =$("input[name=appreciation]").is(":checked");
    var commentabled =$("input[name=commentabled]").is(":checked");
    var description =$("textarea[name=description1]").val();
    //alert(title+" "+content+" "+typeCode+" "+tagCode+" "+firstPicture+" "+recommend+" "+shareStatement+" "+appreciation+" "+commentabled+" "+flag+" "+published+" "+id+" "+blogCode);
    if(title == null || title == ""){
        alert("标题为空");
        return false;
    }
    if(content == null || content == ""){
        alert("内容为空");
        return false;
    }
    if(typeCode == null || typeCode == ""){
        alert("分类为空");
        return false;
    }
    if(tagCode == null || tagCode == ""){
        alert("标签为空");
        return false;
    }
    if(firstPicture == null || firstPicture == ""){
        alert("首图为空");
        return false;
    }
    if(description == null || description == ""){
        alert("描述为空");
        return false;
    }
    $.ajax({
        url:'blog/updateBlog',
        data:JSON.stringify({
            "id":id,
            "code":blogCode,
            "title":title,
            "typeCode":typeCode,
            "content":content,
            "firstPicture":firstPicture,
            "flag":flag,
            "appreciation":appreciation,
            "shareStatement":shareStatement,
            "commentabled":commentabled,
            "published":published,
            "recommend":recommend,
            "description":description,
            "tagCode":tagCode
        }),
        type:"POST",
        dataType:"json",
        contentType:"application/json",
        async:true,
        success:function(data){
            //alert(JSON.stringify(data));
            if (data.code == "200"){
                alert("修改成功！");
                window.location.href='/blogs';
            }
        }
    })
})
$("#publish-bth").on("click",function(){
    $("input[name=published]").val(true);

    var published=$("input[name=published]").val();
    var flag=$("input[name=flag]").val();
    var title =$("input[name=title1]").val();
    var content =$("textarea[name=content]").val();
    var typeCode =$("input[name=tname]").val();
    var tagCode =$("input[name=tag]").val();
    var firstPicture =$("input[name=firstPicture1]").val();
    var id =$("input[name=uid]").val();
    var blogCode =$("input[name=blogCode]").val();
    var recommend =$("input[name=recommend]").is(":checked");
    var shareStatement =$("input[name=shareStatement]").is(":checked");
    var appreciation =$("input[name=appreciation]").is(":checked");
    var commentabled =$("input[name=commentabled]").is(":checked");
    var description =$("textarea[name=description1]").val();
    //alert(title+" "+content+" "+typeCode+" "+tagCode+" "+firstPicture+" "+recommend+" "+shareStatement+" "+appreciation+" "+commentabled+" "+flag+" "+published+" "+id+" "+blogCode);
    if(title == null || title == ""){
        alert("标题为空");
        return false;
    }
    if(content == null || content == ""){
        alert("内容为空");
        return false;
    }
    if(typeCode == null || typeCode == ""){
        alert("分类为空");
        return false;
    }
    if(tagCode == null || tagCode == ""){
        alert("标签为空");
        return false;
    }
    if(firstPicture == null || firstPicture == ""){
        alert("首图为空");
        return false;
    }
    if(description == null || description == ""){
        alert("描述为空");
        return false;
    }
    $.ajax({
        url:'blog/updateBlog',
        data:JSON.stringify({
            "id":id,
            "code":blogCode,
            "title":title,
            "typeCode":typeCode,
            "content":content,
            "firstPicture":firstPicture,
            "flag":flag,
            "appreciation":appreciation,
            "shareStatement":shareStatement,
            "commentabled":commentabled,
            "published":published,
            "recommend":recommend,
            "description":description,
            "tagCode":tagCode
        }),
        type:"POST",
        dataType:"json",
        contentType:"application/json",
        async:true,
        success:function(data){
            //alert(JSON.stringify(data));
            if (data.code == "200"){
                alert("修改成功！");
                window.location.href='/blogs';
            }
        }
    })
})
